package com.yh.Dao.Impl;

import com.yh.Dao.IVoteDao;
import com.yh.entity.Vote;
import com.yh.until.DBuntil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class VoteDaoImpl extends DBuntil implements IVoteDao {
    Connection conn=null;
    PreparedStatement pst=null;
    ResultSet rs=null;
    @Override
    public int addVote(int id) {
        int len=0;
        conn=getConn();
        try {
            String sql="update vote_items set times=times+10 where id=?";
            pst=conn.prepareStatement(sql);
            pst.setInt(1,id);
           len=pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            clossAll(conn,pst,rs);
        }
        return len;
    }

    @Override
    public List showAll() {
        List list=new ArrayList();

        conn=getConn();
        try {
            String sql="select * from vote_items";
            pst=conn.prepareStatement(sql);
            rs=pst.executeQuery();
            while (rs.next()){
                Vote v=new Vote();
                v.setId(rs.getInt(1));
                v.setItem(rs.getString(2));
                v.setTimes(rs.getInt(3));
                list.add(v);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            clossAll(conn,pst,rs);
        }
        return list;
    }
}
